import { reqGetGoodDetailInfoData } from '@/api';

const state = {
  goodDetailInfo: {},
};

const actions = {
  async getGoodDetailInfoData({ commit }, id) {
    const result = await reqGetGoodDetailInfoData(id);
    if (result.code === 200) {
      commit('SAVE_GOOD_DETAIL_INFO_DATA', result.data);
    } else {
      console.log(result.message);
    }
  },
};

const mutations = {
  SAVE_GOOD_DETAIL_INFO_DATA(state, payload) {
    state.goodDetailInfo = payload; // 是个对象形式
  },
  //更改放大镜图片的默认图片链接
  UPDATE_IMG_DEFAULT_URL(state, imgUrl) {
    state.goodDetailInfo.skuInfo.skuDefaultImg = imgUrl;
  },
};
const getters = {
  categoryView(state) {
    return state.goodDetailInfo.categoryView || {};
  },
  skuInfo(state) {
    return state.goodDetailInfo.skuInfo || {};
  },
  spuSaleAttrList(state) {
    return state.goodDetailInfo.spuSaleAttrList || [];
  },
};

export default {
  namespaced: true, // 开启命名空间
  state,
  actions,
  mutations,
  getters,
};
